<template>
    <div class="home-container">
        <van-nav-bar class="page-nav-bar">
            <van-button
            slot="title"
            type="info"
            size="small"
            class="search-btn"
            round
            icon="search"
            to="/search">
                搜索
            </van-button>
        </van-nav-bar>

        <!-- 频道列表 -->
        <van-tabs class="channel-tabs" v-model="active" animated swipeable>
            <van-tab v-for="i in channels" :key="i.id" :title="i.name">
                <ArticleList :channel="i"/>
            </van-tab>

            <div slot="nav-right" class="placeholder"></div>
            <div slot="nav-right" class="hamburger-btn" @click="isChannelEditShow = true">
                <i class="toutiao toutiao-gengduo"></i>
            </div>
        </van-tabs>

        <!-- 频道编辑 弹出层-->
        <van-popup
        v-model="isChannelEditShow"
        round
        closeable
        position="bottom"
        :style="{ height: '80%' }">
            <ChannelEdit :myChannels="channels" :active="active" @changeActive="changeActiveFn"/>
        </van-popup>
    </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './components/ChannelEdit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
    name: 'Home',
    components: {
        ArticleList,
        ChannelEdit
    },
    data () {
        return {
            active: 0,
            channels: [],
            isChannelEditShow: false
        }
    },
    computed: {
        ...mapState(['user'])
    },
    methods: {
        async loadChannels () {
            try {
                let channels = []

                if (this.user) {
                    // 已登录，获取用户频道列表
                    const res = await getUserChannels()
                    channels = res.data.data.channels
                } else {
                    // 未登录，判断本地是否有频道列表数据
                    const localChannels = getItem('TOUTIAO_CHANNELS')
                    if (localChannels) {
                        channels = localChannels
                    } else {
                        // 没有则获取默认列表
                        const res = await getUserChannels()
                        channels = res.data.data.channels
                    }
                }
                this.channels = channels
            } catch (err) {
                this.$toast('获取频道数据失败')
            }
        },
        changeActiveFn (i, isChannelEditShow = true) {
            this.active = i

            // 在未传递第二个参数的情况下，使用默认值true
            this.isChannelEditShow = isChannelEditShow
        }
    },
    created () {
        this.loadChannels()
    }
}
</script>

<style scoped lang="less">
.home-container {
    padding-top: 174px;
    .search-btn {
        width: 555px;
        height: 64px;
        background-color: #5babfb;
        border: 0;
        font-size: 28px;
        .van-icon {
            font-size: 32px;
        }
    }
}

// 在scoped作用域中，样式只能作用于根组件，要想影响其后代组件必须加 /deep/ 或 >>>
/deep/ .channel-tabs {
    .van-tabs__wrap {
        position: fixed;
        top: 92px;
        left: 0;
        right: 0;
        z-index: 1;
        height: 82px;
    }
    .van-tabs__line {
        width: 31px;
        height: 6px;
        background-color: #3296fa;
        bottom: 8px;
    }
    .van-tabs__nav {
        padding-bottom: 0;
    }
    .van-tabs__nav--line.van-tabs__nav--complete {
        padding: 0;
    }
    .van-tab {
        min-width: 200px;
        border-right: 1px solid #edeff3;
        font-size: 30px;
        color: #777777;
    }
    .van-tab--active {
        color: #333333;
    }
    .placeholder {
        // 不参与flex的空间分配
        flex-shrink: 0;
        width: 66px;
        height: 82px;
    }
    .hamburger-btn {
        position: fixed;
        right: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 66px;
        height: 82px;
        background-color: #fff;
        opacity: 0.8;
        i.toutiao {
            font-size: 33px;
        }
        &::before {
            content: "";
            position: absolute;
            left: 0;
            width: 2px;
            height: 100%;
            background: url(~@/assets/gradient-gray-line.png);
            background-size: contain;
        }
    }
}
</style>
